#include "iostream"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

struct StructOutput
{
	int peopleStarved,
		immigrants,
		population,
		rate,
		acresOwned,
		harvest,
		rats,
		totalGrain,
		currentYear;
	bool plague;
	StructOutput(){}
	StructOutput(int a_peopleStarved, int a_immigrants, int a_population, int a_rate, int a_acresOwned, int a_harvest, int a_rats, int a_totalGrain, int a_currentYear, bool a_plague)
	{peopleStarved = a_peopleStarved; immigrants = a_immigrants; population = a_population; rate = a_rate; acresOwned = a_acresOwned; harvest = a_harvest; rats = a_rats; totalGrain = a_totalGrain; currentYear = a_currentYear; plague = a_plague;}
};

struct StructInput
{
	int acres,
		feed,
		plant;
	StructInput(){}
	StructInput(int a_acres, int a_feed, int a_plant)
	{acres = a_acres; feed = a_feed; plant = a_plant;}
};
StructOutput Process(StructOutput aO, StructInput aI)
{
	//initialize random seed
	srand ( time(NULL) );

	//startvation
	int expectedFeed = aO.population * 20;
	float percentOfPopStarved = 0.0f;
	if(aI.feed < expectedFeed)
		percentOfPopStarved = (expectedFeed - aI.feed) / expectedFeed;
	aO.peopleStarved = aO.population * percentOfPopStarved;

	//If percent is over 45% of population, the player loses

	//population growth
	int randVal = (rand() % 5) + 1;
	aO.immigrants = int(((randVal *(20* aO.acresOwned + aO.totalGrain) / aO.population) / 100)+1);
	aO.population += aO.immigrants - aO.peopleStarved;

	//plague
	aO.plague = false;
	int plagueChance = rand() % 100 + 1;
	if(plagueChance < 15)
		aO.plague = true;

	if(aO.plague)
		aO.population *= .5;

	//tradeRate
	aO.rate = 17 + (rand() % 9 + 1);

	//acresOwned
	aO.acresOwned += aI.acres;
	
	//grainBought
//	int cost = aI.acres * aO.rate;
	aO.totalGrain -= (aI.acres * aO.rate);

	//harvest
	aO.harvest = rand() % 5 + 1;
	int grainHarvested = aI.plant * aO.harvest;

	//totalGrain
	aO.totalGrain -= int(aI.plant / 2);

	//rats
	int ratChance = rand() % 5 + 1;
	int amountEaten = 0.0f;
	if(ratChance == 1 || ratChance == 3 || ratChance == 5)
		amountEaten = aO.totalGrain / ratChance;
	aO.totalGrain -= amountEaten;

	//currentYear
	aO.currentYear++;

	return aO;
}
void displayInformation(StructOutput aO)
{
	cout << "In Year " << aO.currentYear << ", " << aO.peopleStarved << " people starved.\n";
	cout << aO.immigrants << " people came to the city.\n";
	cout << "The city population is now " << aO.population << "\n";
	cout << "The city now owns " << aO.acresOwned << " acres.\n";
	cout << "You harvested " << aO.harvest << " bushels per acre.\n";
	cout << "Rats ate " << aO.rats << " bushels.\n";
	cout << "You now have " << aO.totalGrain << " bushels in store.\n";
	cout << "Land is trading at " << aO.rate << " bushels per acre.\n";
}
StructInput userInput()
{
	int a, f, p;
	cout << "\nHow many acres do you wish to buy or sell?\n(enter a negative amount to sell bushels)\n\t";
	cin >> a;
	cout << "How many bushels do you wish to feed your people?\n(each citizen needs 20 bushels a year)\n\t";
	cin >> f;
	cout << "How many acres do you wish to plant with seed?\n(each acre takes one bushel)\n\t";
	cin >> p;
	StructInput temp(a,f,p);
	return temp;
}
void main()
{
	StructInput sInput;
	StructOutput sLastTurn;
	sLastTurn = StructOutput(0,5,100,24,1000,3,200,2800,1,false);
	sInput = StructInput(25,2000,100);
	displayInformation(sLastTurn);
	sInput = userInput();
	
	StructOutput sThisTurn;
	sThisTurn = Process(sLastTurn,sInput);
	
	//if too many people starve, the player looses
	if(sThisTurn.peopleStarved / sLastTurn.population > .45)
	{
		cout<<"Too many people starved!";
	}

	displayInformation(sThisTurn);

}